//
//  ChangePasswordController.m
//  51易借
//
//  Created by 张睿 on 2017/8/28.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "ChangePasswordController.h"

@interface ChangePasswordController ()

@property (nonatomic, weak)UIView * topView;
@property (nonatomic, weak)UIView * inputView;
@property (nonatomic, strong)UILabel * oldPasswordLabel;
@property (nonatomic, strong)UITextField * oldPasswordText;
@property (nonatomic, strong)UIView * oneLineView;
@property (nonatomic, strong)UILabel * NPWLabel;
@property (nonatomic, strong)UITextField * NPWText;
@property (nonatomic, strong)UIView * twoLineView;
@property (nonatomic, strong)UILabel * surePasswordLabel;
@property (nonatomic, strong)UITextField * surePasswordText;
@property (nonatomic, weak)UILabel * showLabel;
@property (nonatomic, strong)UIButton * logoutButton;


@end

@implementation ChangePasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupNavBar];
    [self setupUI];
}


- (void)setupNavBar{
    
    self.title = @"修改登录密码";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToLastView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}


- (void)setupUI{
    
    UIView * topView = [UIView new];
    
    topView.backgroundColor = TOTAL_COLOR;
    
    self.topView = topView;
    
    [self.view addSubview:self.topView];
    
    
    UIView * inputView = [UIView new];
    
    inputView.backgroundColor = [UIColor whiteColor];
    inputView.layer.borderColor = [ColorWithRGB(0xe6e6e6) CGColor];
    inputView.layer.borderWidth = 0.5 * SCREENH6;
    
    self.inputView = inputView;
    
    [self.view addSubview:self.inputView];
    
    
    self.oldPasswordLabel = [self setupLeftLabelWith:@"旧密码"];
    
    [self.inputView addSubview:self.oldPasswordLabel];
    
    self.oldPasswordText = [self setupTextField:@"请输入旧密码"];
    
    [self.inputView addSubview:self.oldPasswordText];
    
    
    self.oneLineView = [UIView new];
    
    self.oneLineView.backgroundColor = BACK_COLOR;
    
    [self.inputView addSubview:self.oneLineView];
    
    
    self.NPWLabel = [self setupLeftLabelWith:@"新密码"];
    
    [self.inputView addSubview:self.NPWLabel];
    
    self.NPWText = [self setupTextField:@"请输入新密码"];
    
    [self.inputView addSubview:self.NPWText];
    
    
    self.twoLineView = [UIView new];
    
    self.twoLineView.backgroundColor = BACK_COLOR;
    
    [self.inputView addSubview:self.twoLineView];
    
    
    self.surePasswordLabel = [self setupLeftLabelWith:@"确认新密码"];
    
    [self.inputView addSubview:self.surePasswordLabel];
    
    self.surePasswordText = [self setupTextField:@"请再次输入新密码"];
    
    [self.inputView addSubview:self.surePasswordText];
    
    
    UILabel * showLabel = [UILabel new];
    
    showLabel.text = @"登录密码由6~18位数字和字母组成";
    showLabel.textColor = ColorWithRGB(0x999999);
    showLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    showLabel.textAlignment = NSTextAlignmentCenter;
    
    self.showLabel = showLabel;
    
    [self.view addSubview:self.showLabel];
    
    
    UIButton * logoutButton = [UIButton new];
    //300 * 42
    [logoutButton setTitle:@"确认修改" forState:UIControlStateNormal];
    [logoutButton setBackgroundImage:[UIImage imageNamed:@"登录"] forState:UIControlStateNormal];
    [logoutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [logoutButton addTarget:self action:@selector(didChangeButton) forControlEvents:UIControlEventTouchUpInside];
    logoutButton.titleLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    
    self.logoutButton = logoutButton;
    
    [self.view addSubview:self.logoutButton];
}



- (void)viewDidLayoutSubviews{
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(64);
    }];
    
    [self.inputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(6 * SCREENH6);
        make.top.mas_equalTo(self.topView.mas_bottom).offset(11 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-6 * SCREENH6);
        make.height.mas_equalTo(146 * SCREENH6);
    }];
    
    [self.oldPasswordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.inputView).offset(17 * SCREENH6);
        make.top.mas_equalTo(self.inputView).offset(15 * SCREENH6);
    }];
    
    [self.oldPasswordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.oldPasswordLabel.mas_trailing).offset(55 * SCREENH6);
        make.centerY.mas_equalTo(self.oldPasswordLabel);
        make.width.mas_equalTo(250 * SCREENH6);
        make.height.mas_equalTo(40 * SCREENH6);
    }];
    
    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(self.inputView).offset(48 * SCREENH6);
        make.leading.mas_equalTo(self.inputView).offset(5 * SCREENH6);
        make.trailing.mas_equalTo(self.inputView).offset(-5 * SCREENH6);
    }];
    
    [self.NPWLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.oldPasswordLabel);
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(17 * SCREENH6);
    }];
    
    [self.NPWText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.oldPasswordText);
        make.centerY.mas_equalTo(self.NPWLabel);
    }];
    
    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(48 * SCREENH6);
        make.leading.trailing.height.mas_equalTo(self.oneLineView);
    }];
    
    [self.surePasswordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.NPWLabel);
        make.top.mas_equalTo(self.twoLineView.mas_bottom).offset(17 * SCREENH6);
    }];
    
    [self.surePasswordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.NPWText);
        make.centerY.mas_equalTo(self.surePasswordLabel);
    }];
    
    [self.showLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.inputView.mas_bottom).offset(20 * SCREENH6);
    }];
    
    [self.logoutButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.showLabel.mas_bottom).offset(25 * SCREENH6);
        make.width.mas_equalTo(300 * SCREENH6);
        make.height.mas_equalTo(42 * SCREENH6);
    }];
}


- (UILabel *)setupLeftLabelWith:(NSString *)title{
    
    UILabel * label = [UILabel new];
    
    label.font = [UIFont systemFontOfSize:14 * SCREENH6];
    label.textColor = ColorWithRGB(0x303c41);
    label.textAlignment = NSTextAlignmentLeft;
    label.text = title;
    
    return label;
}


- (UITextField *)setupTextField:(NSString *)name {
    
    
    UITextField * textFiled = [[UITextField alloc] init];
    
    textFiled.borderStyle = UITextBorderStyleNone;
    textFiled.backgroundColor = [UIColor clearColor];
    //设置光标颜色
    textFiled.tintColor = ColorWithRGB(0xa5a5a5);
//    textFiled.delegate = self;
    
//    textFiled.background = [UIImage imageNamed:name];
//    textFiled.textAlignment = UITextAlignmentCenter;
    
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0xa5a5a5);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:name attributes:attrs];
    textFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14 * SCREENH6];
    textFiled.textColor = ColorWithRGB(0x303c41);
    textFiled.secureTextEntry = YES;
    
    return textFiled;
}


- (void)didChangeButton{
    
    if(self.oldPasswordText.text.length==0)
    {
        [self showHint:@"请输入旧密码"];
        return;
    }
    if(self.NPWText.text.length == 0)
    {
        [self showHint:@"请新输入密码"];
        return;
    }
    if(self.surePasswordText.text.length==0)
    {
        [self showHint:@"请再次输入密码"];
        return;
    }
    
    if (![self checkPassWordWith:self.NPWText.text] || ![self checkPassWordWith:self.surePasswordText.text]) {
        [self showHint:@"请输入6~18位数字和字母组合"];
        return;
    }
    
    if(![self.NPWText.text isEqualToString:self.surePasswordText.text])
    {
        [self showHint:@"两次输入密码不一致"];
        return;
    }
    
    
    NSDictionary * dic = @{@"newPassword":[self md5:self.surePasswordText.text],@"oldPassword":[self md5:self.oldPasswordText.text]};
    [[NetworkTool shareManager] POST:[Interfacefile modifyPassword] Param:dic andWaitStr:@"" waitView:self.view success:^(id  _Nullable responseObject) {
        if([[responseObject objectForKey:@"code"]intValue]==0)
        {
//            [self showHint:@"修改成功"];
//            [self.navigationController popViewControllerAnimated:YES];
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"修改成功"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      [self.navigationController popViewControllerAnimated:YES];
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
            
            [self showHint:[responseObject objectForKey:@"message"]];
            
            
        }
    } andFailure:^(NSError * _Nonnull error) {
        [self showHint:@"网络错误"];
    }];
    
    
    
}

//MD5 加密
- (NSString *) md5:(NSString *) input {
    const char *cStr = [input UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call
    
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return  output;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.oldPasswordText resignFirstResponder];
    [self.NPWText resignFirstResponder];
    [self.surePasswordText resignFirstResponder];
}


- (void)backToLastView{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(BOOL)checkPassWordWith:(NSString *)string
{
    //6-20位数字和字母组成
    NSString *regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$";
    NSPredicate *   pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    if ([pred evaluateWithObject:string]) {
        return YES ;
    }else
        return NO;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
